package com.yy.yyui.progressbar
{
	import com.siku.manor.common.Sprintf;
	import com.siku.manor.common.TimeFormat;
	import com.siku.manor.config.Sys;
	import com.siku.manor.view.common.ConsignmentSwitchBut;
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.GT;
	import com.siku.manor.view.component.text.TextLabel;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextFormatAlign;

	public class ProgressBar extends Sprite
	{
		private var w:Number,h:Number;
		
		private var bg:DisplayObject;
		private var _bar:DisplayObject;
		private var tl:TextLabel;
		private var barMask:Shape;
		
		private var _total:Number = 100;
		private var _progress:Number = 100;
		
		private var _type:Object;
			
		public static const TYPE_TIME_HHMMSS:Object = 0;
		public static const TYPE_NORMAL:Object = 1;
		public static const NONE:int = 0x7fffff;
						
		public function ProgressBar(dg:DisplayObject = null)
		{
			//TODO: implement function
			super();
			
			//type
			this._type = TYPE_TIME_HHMMSS;
			
			//background
			bg = UM.scale9(new ConsignmentSwitchBut());
			this.addChild(bg);

			
			w = bg.width;
			h = bg.height;
			
			
			//bar
			if(dg == null)
			{
				_bar = UM.scale9(new ProgressBarSkin());				
			}else
			{
				_bar = UM.scale9(dg);
			}
			bar.x = 1;
			bar.y = 1;
			bar.width = bg.width-2;
			bar.height = this.h-2;
			this.addChild(bar);
			
			//bar mask
			this.barMask = new Shape();
			this.addChild(this.barMask);
			bar.mask = this.barMask;

			//textfield
			tl = new TextLabel();
			tl.y = 1;
			tl.width = this.width;
			tl.textFormat.align = TextFormatAlign.CENTER;
			tl.textFormat.size = 11;
			tl.text = "";
			this.addChild(tl);
			
			this.updateBar();
			tl.text = "";
		}
		
		public function get type():Object{return this._type;}
		public function set type(value:Object):void{
			this._type = value;
			updateBar();
		}
		
		public function updateBar():void{
			if(this.progress<0){
				this._progress = 0;
			}else if(this.progress>_total){
				this._progress = _total;
			}			
			var g:Graphics = this.barMask.graphics;
			g.clear();
			if(this.total == 0) return;
			var barW:Number = (this.progress*(this.width-3))/this.total;
			//this.bar.width = barW;
			g.beginFill(0xff);
			g.drawRect(this.bar.x,this.bar.y,barW,this.bar.height);
			g.endFill();
			var output:String = "";
			switch(type){
				case TYPE_TIME_HHMMSS:
					var formatString:String;
					if(this.progress>=3600){
						output = TimeFormat.format(this.progress,TimeFormat.STANDARDFORMAT);
					}else{
						output = TimeFormat.format(this.progress,TimeFormat.STANDARDFORMAT.substring(2));
					}
					break;
				case TYPE_NORMAL:
				//／
					output = Sprintf.format("%s/%s",this.progress.toString(),this.total.toString());
					break;
				case NONE:
				default:
					output = tl.text;
					break;
			}
			this.tl.text = output;
		}
		
		public function setGreenBar():void{
			this.bar.filters = null;
		}
		
		public function setBlueBar():void{
			this.bar.filters = Sys.RED_BLUE_FILTER;
		}
		
		public function set bar(value:DisplayObject):void{
			GT.replaceChild(this,value,this._bar);
			this._bar = value;
			this._bar.mask = this.barMask;
		}
		public function get bar():DisplayObject{return this._bar;}
		
		
		override public function get width():Number{
			return w;
		}
		
		override public function get height():Number{
			return h;
		}
		
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			this.bg.width+=diff;
			this.tl.width+=diff;
			this.bar.width+=diff;
			w = newWidth;
			updateBar();
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			this.bg.height+=diff;
			this.bar.height+=diff;
			this.tl.y+=diff/2;
			h = newHeight;			
		}
		
		public function get total():Number{
			return _total;
		}
		
		public function set total(value:Number):void{
			this._total = value;
		}
		
		public function get progress():Number{
			return _progress;
		}
		
		public function set progress(value:Number):void{
			this._progress = value;
			updateBar();
		}
		
		public function set percent(value:Number):void{
			this.progress = (total == 0)?0:(value*this.total);
		}
		
		public function set text(value:String):void{
			this.tl.text = value;
		}
		
		public function set textShow(value: Boolean): void{
			this.tl.visible = value;
		}
		
	}
}